/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



package javadrone.util;

//~--- non-JDK imports --------------------------------------------------------

import gnu.io.*;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SerialPortHandler {
    private InputStream  inStream;
    private OutputStream outStream;
    private SerialPort   serialPort;

    public static void main(String args[]) {
        try {
            SerialPortHandler sph = new SerialPortHandler();

            sph.list();
            sph.connect("COM4");
        } catch (IOException ex) {
            Logger.getLogger(SerialPortHandler.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    public void list() {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();

        while (ports.hasMoreElements()) {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();

            System.out.println(port.getName());
        }
    }

    public void connect(String portName) throws IOException {
        try {

            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort = (SerialPort) portId.open("Demo application", 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection. If they won't
            // open, close the port before throwing an exception.
            outStream = serialPort.getOutputStream();
            inStream  = serialPort.getInputStream();
        } catch (NoSuchPortException | PortInUseException e) {
            e.printStackTrace();

            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();

            throw e;
        }
    }

    /**
     *
     * Get the serial port input stream
     * 35
     * @return The serial port input stream
     * 36
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     *
     * Get the serial port output stream
     * 43
     * @return The serial port output stream
     * 44
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * 50
     * Sets the serial port parameters
     * 51
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 57600;    // 57600bps

        try {

            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                           SerialPort.PARITY_NONE);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException("Unsupported serial port parameter");
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
